package alp.starcode.nudtportal.framework.config.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 初始化rabbitmq连接
 *
 * @Author wj
 * @Date 2023/8/1
 * @Version 1.0
 */
@Component
@Slf4j
public class ConnectionFactoryInit implements ApplicationRunner {

    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;

    @Qualifier("getRabbitAdmin")
    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private RabbitMqApi rabbitMqApi;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //创建vhost
        rabbitMqApi.addVhost(virtualHost);

        //vhost授权
        String configure = ".*";
        String read = ".*";
        String write = ".*";
        rabbitMqApi.addVhostPermissions(virtualHost, configure, read, write);

        log.info("创建rabbitmq连接");
        rabbitAdmin.getQueueProperties("init");
        log.info("创建rabbitmq连接完成");
    }
}
